package com.example.train_operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.train_operate.dto.TaskDTO;
import com.example.train_operate.dto.UserDTO;
import com.example.train_operate.entity.Cabinet;
import com.example.train_operate.service.CabinetService;
import com.example.train_operate.mapper.CabinetMapper;
import com.example.train_operate.util.Result;
import com.example.train_operate.util.UserHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.example.train_operate.util.FileUrlConstants.FILE_URL;

/**
* @author 21609
* @description 针对表【tb_cabinet】的数据库操作Service实现
* @createDate 2025-08-13 17:06:39
*/
@Service
public class CabinetServiceImpl extends ServiceImpl<CabinetMapper, Cabinet>
    implements CabinetService{

    @Resource
    private CabinetMapper cabinetMapper;
    /**
     * 添加机柜任务
     * @param cabinet
     * @return
     */
    @Override
    public Result addCabinet(Cabinet cabinet) {
        try{
            UserDTO user = UserHolder.getUser();
            Integer userId = user.getId();
            cabinet.setUserId(userId);
            String photoUrl = cabinet.getPhotoUrl();
            if(photoUrl != null && !photoUrl.isEmpty()){
                String substring = photoUrl.substring(FILE_URL.length());
                cabinet.setPhotoUrl(substring);
            }
            cabinetMapper.insert(cabinet);
            QueryWrapper<Cabinet> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId)
                    .orderByDesc("create_time")
                    .last("limit 1");
            Cabinet cabinet1 = cabinetMapper.selectOne(queryWrapper);
            return Result.ok(cabinet1).message("机柜及柜内设备外观检查完成");
        }catch (Exception e){
            return Result.fail().message("出错了");
        }
    }

    /**
     * 根据机柜id查询机柜信息
     * @param cabinetId
     * @return
     */
    @Override
    public Result getCabinetById(Integer cabinetId) {
        try{
            QueryWrapper<Cabinet> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("cabinet_id", cabinetId)
                    .eq("is_delete", 0);
            Cabinet cabinet = cabinetMapper.selectOne(queryWrapper);
            if(cabinet == null){
                return Result.fail().message("没有此条机柜检修信息");
            }
            String url = cabinet.getPhotoUrl();
            if(url != null && !url.isEmpty()){
                String photoUrl = FILE_URL + url;
                cabinet.setPhotoUrl(photoUrl);
            }
            return Result.ok(cabinet);
        }catch (Exception e){
            return Result.fail();
        }
    }

    /**
     * 修改机柜任务信息
     * @param cabinet
     * @return
     */
    @Override
    public Result updateCabinet(Cabinet cabinet) {
        try{
            String photoUrl = cabinet.getPhotoUrl();
            if(photoUrl != null && !photoUrl.isEmpty()){
                String substring = photoUrl.substring(FILE_URL.length());
                cabinet.setPhotoUrl(substring);
            }
            cabinetMapper.updateById(cabinet);
            Cabinet cabinet1 = cabinetMapper.selectById(cabinet.getCabinetId());
            return Result.ok(cabinet1).message("修改成功");
        }catch (Exception e){
            return Result.fail().message("修改失败");
        }
    }


}




